package com.guoguo.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.guoguo.base.AuthorityResource;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/18 0018.
 */
public class AuthorityValidateUtils {

    /**
     * 获得权限集
     *
     * @param permission
     * @return
     */
    public static JSONObject getAuthority(String permission) {
        JSONObject jsonObject = JSONObject.parseObject(permission);
        JSONObject roles = new JSONObject();
        if (validate(permission)) {
            //判断权限字段里有没有权限
            if (jsonObject.get(AuthorityResource.PERMISSION_KEY).equals(AuthorityResource.AUTORITY_AUTHC)) {

                JSONArray jsonArray = jsonObject.getJSONArray(AuthorityResource.ROLE_KEY);
                Iterator iterator = jsonArray.iterator();
                while (iterator.hasNext()) {
                    JSONObject role = (JSONObject) iterator.next();
                    Iterator<String> keys = role.keySet().iterator();
                    while (keys.hasNext()) {
                        String key = keys.next();
                        roles.put(key, role.get(key));
                    }
                }
            } else {
                roles.put(AuthorityResource.AUTORITY_ANON, AuthorityResource.OPERATING_VIEW);
            }
        }
        return roles;
    }

    /**
     * 权限验证
     * 是否有权限
     *
     * @param permission
     * @return
     */
    public static boolean validate(String permission) {
        try {
            JSONObject jsonObject = JSONObject.parseObject(permission);
            if (jsonObject.get(AuthorityResource.PERMISSION_KEY).equals(AuthorityResource.AUTORITY_NONE)) {
                return false;
            } else {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 生成权限字符串
     *
     * @param permission
     * @param roleOper
     * @return
     */
    public static String createAuth(String permission, Map<String, List<String>> roleOper) {
        JSONObject jsonObject = new JSONObject();
        if (permission != null && !"".equals(permission)) {
            jsonObject.put(AuthorityResource.PERMISSION_KEY, permission);
        }
        if (roleOper != null && roleOper.size() > 0) {
            Iterator<String> keys = roleOper.keySet().iterator();
            JSONArray role = new JSONArray();
            while (keys.hasNext()) {
                String key = keys.next();
                List<String> oper = roleOper.get(key);
                JSONArray operArr = (JSONArray) JSONArray.toJSON(oper);
                JSONObject roleOperJson = new JSONObject();
                roleOperJson.put(key, operArr);
                role.add(roleOperJson);
            }

            jsonObject.put(AuthorityResource.ROLE_KEY, role);
        }
        return jsonObject.toJSONString();
    }


}
